package com.xiaochun.jiankangapp.activity

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.SpannedString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import androidx.lifecycle.lifecycleScope
import com.xiaochun.jiankangapp.R
import com.xiaochun.jiankangapp.base.AppContext
import com.xiaochun.jiankangapp.base.BaseActivity
import com.xiaochun.jiankangapp.databinding.ActivityRegBinding
import com.xiaochun.jiankangapp.extension.onClick
import com.xiaochun.jiankangapp.extension.showToast
import com.xiaochun.jiankangapp.extension.start
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class RegActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewBinding = setContentView<ActivityRegBinding>()
        viewBinding.go.text = SpannableStringBuilder().let {
            it.append("已有账号，")
            it.append(SpannableString("去登录").apply {
                setSpan(
                    ForegroundColorSpan(getColor(R.color.color_primary)),
                    0,
                    length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            })
        }
        viewBinding.go.onClick {
            AppContext.globalContext().let {
                it.finish(LoginActivity::class.java)
            }
            start(LoginActivity::class.java)
            finish()
        }
        viewBinding.login.onClick {
            val phone = viewBinding.account.text.toString()
            val pass = viewBinding.pass.text.toString()
            val code = viewBinding.code.text.toString()
            if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(code)) {
                showToast("请输入手机号/密码/验证码")
                return@onClick
            }
            showLoading()
            loginViewModel.reg(phone, pass, code) {
                hideLoading()
                if (it.isSuccessful) {
                    showToast("注册成功")
                    AppContext.globalContext().let {
                        it.finish(LoginActivity::class.java)
                        it.finish(MainActivity::class.java)
                    }
                    start(MainActivity::class.java)
                    finish()
                } else {
                    showToast("注册失败，" + it.msg)
                }

            }
        }
        viewBinding.getcode.onClick {
            val phone = viewBinding.account.text.toString()
            if (TextUtils.isEmpty(phone)) {
                showToast("请输入手机号")
                return@onClick
            }
            viewBinding.getcode.isEnabled = false
            showLoading()
            loginViewModel.sendCode(phone) {
                hideLoading()
                if (it.isSuccessful) {
                    countDown(viewBinding)
                } else {
                    viewBinding.getcode.isEnabled = true
                    showToast("获取失败，" + it.msg)
                }
            }
        }
    }

    private fun countDown(viewBinding: ActivityRegBinding) {
        lifecycleScope.launch {
            var max = 60
            viewBinding.getcode.isEnabled = false
            while (isActive && max > 0) {
                viewBinding.getcode.text = max.toString() + "s"
                max -= 1
                delay(1000)
            }
            viewBinding.getcode.text = "获取验证码"
            viewBinding.getcode.isEnabled = true
        }
    }
}